<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        ul,
        li {
            list-style: none;
        }

        a {
            color: inherit;
            text-decoration: none;
        }

        img {
            vertical-align: top;
        }

        .clear::after {
            content: "";
            display: block;
            clear: both;
            height: 0;
            visibility: hidden;
            overflow: hidden;
        }

        .product-intro {
            width: 1226px;
            min-height: 500px;
            margin: 0 auto;
            margin-top: 50px;

            display: flex;
            justify-content: space-between;

            /* background-color: grey; */
        }

        .product-intro .preview-wrap {
            width: 400px;
            /* background-color: pink; */
            position: relative;
        }

        .product-intro .preview-wrap .preview {
            width: 400px;
            height: 400px;
            position: relative;
        }

        .product-intro .preview-wrap .preview img {
            width: 100%;
        }

        .product-intro .preview-wrap .preview .shadow {
            width: 200px;
            height: 200px;
            position: absolute;
            left: 0;
            top: 0;
            background-color: rgba(0, 0, 0, 0.3);
            display: none;
        }

        .product-intro .preview-wrap .preview-scale {
            width: 500px;
            height: 500px;
            position: absolute;
            left: -webkit-calc(100% + 10px);
            top: 0;
            background-color: rgba(125, 255, 255, 0.6);
            overflow: hidden;
            display: none;
        }

        .product-intro .preview-wrap .preview-scale img {
            width: 1000px;
            height: 1000px;
            position: absolute;
            left: 0;
            top: 0;
            /* z-index: -1; */
        }

        .product-intro .itemInfo-wrap {
            margin-left: 10px;
            flex: 1;
            /* background-color: #6ff; */
        }
    </style>
</head>

<body>
    <!-- magnifier -->
    <div class="product-intro">
        <!--relative-->
        <div class="preview-wrap">

            <div class="preview">
                <img class="smallImg" src="../images/girlsmall1.jpg" alt="">
                <div class="shadow"></div>
            </div>

            <!-- absolute-->
            <div class="preview-scale">
                <img class="bigImg" src="../images/girlbig1.jpg" alt="">
            </div>

            <!-- <ul class="preview-list">
                <li><img src="" alt=""></li>
                <li><img src="" alt=""></li>
                <li><img src="" alt=""></li>
                <li><img src="" alt=""></li>
            </ul> -->
        </div>
        <div class="itemInfo-wrap"></div>
    </div>
</body>
<script>


    // 在preview中实现shadow跟随鼠标鼠标的效果?
    // 1. 先知道鼠标的位置
    // 

    /* 
        放大镜实现流程
        1. 呈比例关系放大  (=> bigImg大小)
           shadow(200)           smallImg(400)
        —————————————————   =  —————————————————
        previewScale(500)         bigImg

        shadow遮住smallImg的区域 就是 previewScale显示的区域

        2. 左边移动的shadow(定位),右边移动的是bigImg( => bigImg定位)

        3. shadow移动的距离 和 bigImg移动的距离 是等比例且反向的 (1 : 2.5)
    
    */

    /* 
        放大镜添加 移入显示 移出隐藏出现问题
        1.  右边大图片不动了  
            分析: 大图片不能动(-scale*x) 但是shadow可以整除移动  =>  scale有问题 
            
        2.  shadow可以超出边界
            分析:    shadow可以超出边界 -> 边界maxLeft maxTop计算有问题


        原因: 无论是scale的问题, 边界maxLeft maxTop计算有问题 => 都是同一个问题
        shadow 和 previewScale(>bigImg)  -> 隐藏了(display:none) -> clientWidth clientHeight 不生效 => 0


        解决方法:
        1. 既然clientWidth clientHeight(快捷取值)不能用了 就使用传统的获取元素宽高的方法
        getComputedStyle => 封装

        2. clientWidth clientHeight  对于隐藏的元素不生效, 等元素显示之后在获取
    
    */


    var previewWrap = document.getElementsByClassName("preview-wrap")[0];
    var preview = document.getElementsByClassName("preview")[0];
    var shadow = document.getElementsByClassName("shadow")[0];
    var previewScale = document.getElementsByClassName("preview-scale")[0];
    var smallImg = document.getElementsByClassName("smallImg")[0];
    var bigImg = document.getElementsByClassName("bigImg")[0];

    var maxLeft = getValue(preview, "width") - getValue(shadow, "width");
    var maxTop = getValue(preview, "height") - getValue(shadow, "height");

    console.log(maxLeft);
    console.log(maxTop);

    var scale = getValue(bigImg, "width") / getValue(smallImg, "width")
    console.log(scale);  // 2.5

    preview.onmouseenter = function () {
        shadow.style.display = "block";
        previewScale.style.display = "block";
    }

    preview.onmousemove = function (e) {
        var e = e || window.event;

        var x = e.pageX - previewWrap.offsetLeft - shadow.clientWidth / 2;
        var y = e.pageY - previewWrap.offsetTop - shadow.clientHeight / 2;

        if (x < 0) x = 0;
        if (x > maxLeft) x = maxLeft;
        if (y < 0) y = 0;
        if (y > maxTop) y = maxTop;

        shadow.style.left = x + "px";
        shadow.style.top = y + "px";

        bigImg.style.left = -(scale * x) + "px";
        bigImg.style.top = -(scale * y) + "px";

    }

    preview.onmouseleave = function () {
        shadow.style.display = "none";
        previewScale.style.display = "none";
    }


    //  获取非行内样式 (万金油  内部样式 外部样式 内联样式 + 默认)
    function getStyle(ele, key) {
        if (window.getComputedStyle) {
            return window.getComputedStyle(ele)[key];
        } else {
            return ele.currentStyle[key];
        }
    }

    // 二次封装
    // 在 getStyle的基础上 加上 去单位操作(需要去单位可以调用此方法)
    function getValue(ele, key) {
        return parseFloat(getStyle(ele, key));
    }

</script>

</html>